<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing csv files.
 * @package backend-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Screening extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('cms');
        
        $this->load->model('screening_main_categories_model');
        $this->load->model('screening_main_category_videos_model');
        $this->load->model('screening_main_sub_categories_model');
        $this->load->model('screening_sub_categories_model');
        $this->load->model('screening_sub_category_videos_model');
        $this->load->model('screening_type_main_categories_model');
        $this->load->model('screening_types_model');
        $this->load->model('screening_videos_model');
        $this->load->model('videos_model');

        $this->load->library('csvreader');
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');

    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        set_time_limit(0);

        $topic = 1;
        $joint = 1;
        $period = 1;
        
        $this->csvreader->separator = '|';
        $screenings = $this->csvreader->parse_file('csvs/Screenings.csv');

        foreach($screenings as $screening)
        {
            // screening_video
            $video_id = $this->videos_model->attribute( array( 'Code' => trim($screening['video_code']) ), 'VideoID')->get();
            if( $video_id )
            {
                
                $screening_period                   = (int) trim( $screening['period']);
                $period                             = $screening_period ? $screening_period : $period;
                
                $screening_video                    = array();
                $screening_video['VideoID']         = $video_id;
                $screening_video['Period']          = $period;
                
                $screening_video_push                    = array();
                $screening_video_push['VideoID']         = $video_id;
                $screening_video_push['Period']          = $period;
                $screening_video_push['DateCreated']     = date('Y-n-d H:i:s');
                $screening_video_id                      = $this->screening_videos_model->push($screening_video, $screening_video_push);
            }

            // topic - type
            $topic_text                             = trim( $screening['topic'] );
            if( $topic_text )
            {
                $topic                              = array();
                $topic['Name']                      = $topic_text;
                $topic['DateCreated']               = date('Y-n-d H:i:s');
                $topic_id = $this->screening_types_model->push( array('Name' => $topic_text), $topic);
            }
            
            // joint - main category
            $joint_text                             = trim( $screening['joint'] );
            if( $joint_text )
            {
                $joint                              = array();
                $joint['Name']                      = $joint_text;
                $joint['DateCreated']               = date('Y-n-d H:i:s');
                $joint_id = $this->screening_main_categories_model->push( array('Name' => $joint_text), $joint);
            }
            
            // type_main
            if( $topic_id && $joint_id )
            {
                $main_type                               = array();
                $main_type['ScreeningMainCategoryID']    = $joint_id;
                $main_type['ScreeningTypeID']            = $topic_id;
                $main_type['DateCreated']                = date('Y-n-d H:i:s');
                $this->screening_type_main_categories_model->push(array('ScreeningMainCategoryID' => $joint_id, 'ScreeningTypeID' => $topic_id), $main_type);
            }
            
            // sub
            $sub_text                              = trim( $screening['screening_position'] );
            if( $sub_text )
            {
                $screening_code                    = trim($screening['screening_code']);
                $code                              = ( $screening_code != '' || $screening_code != '0' ) ? $screening_code : NULL;
                
                $sub                              = array();
                $sub['Name']                      = $sub_text;
                $sub['Code']                      = $code;
                $sub['DateCreated']               = date('Y-n-d H:i:s');
                $sub_id = $this->screening_sub_categories_model->push( array('Name' => $sub_text, 'Code' => $code), $sub);
                
                // main sub
                $main_sub                               = array();
                $main_sub['ScreeningMainCategoryID']    = $joint_id;
                $main_sub['ScreeningSubCategoryID']     = $sub_id;
                $main_sub['DateCreated']                = date('Y-n-d H:i:s');
                $this->screening_main_sub_categories_model->push(array('ScreeningMainCategoryID' => $joint_id, 'ScreeningSubCategoryID' => $sub_id), $main_sub);
                
                // sub videos
                $sub_video                               = array();
                $sub_video['ScreeningSubCategoryID']     = $sub_id;
                $sub_video['ScreeningVideoID']           = $screening_video_id;
                $sub_video['DateCreated']                = date('Y-n-d H:i:s');
                $this->screening_sub_category_videos_model->push(array('ScreeningSubCategoryID' => $sub_id, 'ScreeningVideoID' => $screening_video_id), $sub_video);

            } else
            {
                // main videos
                $main_video                               = array();
                $main_video['ScreeningMainCategoryID']    = $joint_id;
                $main_video['ScreeningVideoID']           = $screening_video_id;
                $main_video['DateCreated']                = date('Y-n-d H:i:s');
                $this->screening_main_category_videos_model->push(array('ScreeningMainCategoryID' => $joint_id, 'ScreeningVideoID' => $screening_video_id), $main_video);
            }
            
        }

        echo '<pre>';
        print_r($screenings); 
        echo '</pre>';
        exit();
        
    }
}

/* End of file parsecsv.php */
/* Location: ./application/roles/backend-csvreader/controllers/parsecsv.php */